package de.uni.muenster.idw2;

import java.util.ArrayList;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import de.uni.muenster.idw2.adapter.LastBuildingsAdapter;
import de.uni.muenster.idw2.data.BBBuilding;
import de.uni.muenster.idw2.utilities.Utils;

public class LastBuildingList extends Activity{
	
	private ListView listViewLastBuildings;
	private LastBuildingsAdapter adapter;
	private ProgressBar progressBar;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.building_list);
		
		/**
		 * loading the listView
		 */
		listViewLastBuildings = (ListView) findViewById(R.id.listViewLastBuildings);
		progressBar = (ProgressBar) findViewById(R.id.title_refresh_progress);
		
		
		final ArrayList<BBBuilding> buildingList = (ArrayList<BBBuilding>) getIntent().getExtras().getSerializable(Utils.BUILDING_INTENT_MSG);
		
		
		String[] buildings = new String[buildingList.size()];
		
		for(int i = 0; i < buildingList.size() ; i++){
			buildings[i] = buildingList.get(i).getName();
		}
		
		System.out.println();
		
		adapter = new LastBuildingsAdapter(this, buildingList, progressBar);
		listViewLastBuildings.setAdapter(adapter);
		
		listViewLastBuildings.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapter, View view, int pos, long arg3) {
				
				Intent intent = new Intent(LastBuildingList.this, BuildingMap.class);
				intent.putExtra(BuildingChooserMap.MAP_ACTIVITY_RESULT_DATA, buildingList.get(pos));
		    	startActivity(intent);
		    	LastBuildingList.this.finish();
			}
		});
	}
	
	@Override
	protected void onPause() {
		super.onPause();
		finish();
	}
	
	@Override
	protected void onStop() {
		super.onStop();
		adapter.stop();
	}
}
